//
//  PayManager.m
//  FashionCoachTest
//
//  Created by px on 16/12/21.
//  Copyright © 2016年 zsr. All rights reserved.
//

#import "PayManager.h"
#import "WXApiObject.h"
#import "WXApi.h"
#import "AppDelegate.h"

@implementation PayManager

+ (id)sharePayManager
{
    static dispatch_once_t once_t;
    static PayManager *manager = nil;
    dispatch_once(&once_t, ^{
        
        manager = [[PayManager alloc]init];
    });
    return manager;
}

- (instancetype)init
{
    self = [super init];
    if (self) {

    }
    return self;
}

/*
 支付请求
 */
- (void)startPayRequest:(NSInteger)payType Pid:(NSString *)pid
{
    if (payType == 0)
    {
        if(![WXApi isWXAppInstalled]) // 判断 用户是否安装微信
        {
            [LTools showMBProgressWithText:@"请先安装微信客户端" addToView:sharedDelegate.window];
            return;
        }
    }
    __weak PayManager * manager = [PayManager sharePayManager];
    NSDictionary *dic = @{@"tradeNo":pid};
    [HYBaseHandler requestPayParmers:dic PayType:payType Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            //微信
            NSDictionary *dic = obj[@"data"];
            if (payType == 0)
            {
                 [manager startWXPay:dic[@"timeStamp"] withPartnerId:dic[@"partnerId"] withPrepayId:dic[@"prepayId"] withNonceStr:dic[@"nonceStr"] withPackage:dic[@"packageValue"] withSign:dic[@"sign"]];
            }
            else if (payType == 1)
            {
                [manager startAliPay:obj];
            }
           

        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:sharedDelegate.window];
        }
    } failed:^(NSError *error) {

    }];
    

}

- (void)startNewPayRequest:(NSInteger)payType Pid:(NSString *)pid
{
    if (payType == 0)
    {
        if(![WXApi isWXAppInstalled]) // 判断 用户是否安装微信
        {
            [LTools showMBProgressWithText:@"请先安装微信客户端" addToView:sharedDelegate.window];
            return;
        }
    }
    __weak PayManager * manager = [PayManager sharePayManager];
    NSDictionary *dic = @{@"tradeNo":pid};
    NSString *url = SERVER_NewWechatPay;
    if (payType == 1)
    {
        url = SERVER_NewAliPay;
    }
    [HYBaseHandler requestWithTheFormUrl:url Parmers:dic Success:^(id obj) {
        if ([obj server_Getcodecheck])
        {
            //微信
            NSDictionary *dic = obj[@"data"];
            if (payType == 0)
            {
                [manager startWXPay:dic[@"timeStamp"] withPartnerId:dic[@"partnerId"] withPrepayId:dic[@"prepayId"] withNonceStr:dic[@"nonceStr"] withPackage:dic[@"packageValue"] withSign:dic[@"sign"]];
            }
            else if (payType == 1)
            {
                [manager startAliPay:obj];
            }
        }
        else
        {
            [LTools showMBProgressWithText:obj[@"msg"] addToView:sharedDelegate.window];
        }
    } failed:^(NSError *error) {
        
    }];

}






/*
 调用支付宝支付
 */
- (void)startAliPay:(NSDictionary *)order
{
    // NOTE: 调用支付结果开始支付
    NSString *appScheme = @"PrintPort";
    NSString * str = order[@"data"];
    [[AlipaySDK defaultService] payOrder:str fromScheme:appScheme callback:^(NSDictionary *resultDic) {
        NSLog(@"reslut = %@",resultDic);
        int resultStatus=[[resultDic objectForKey:@"resultStatus"] intValue];
        __weak AppDelegate *appDelegate = (AppDelegate*)[[UIApplication sharedApplication] delegate];
        if (resultStatus==9000)
            
        {
            [LTools showMBProgressWithText:@"付款成功" addToView:appDelegate.window];
            NSNotification * notice = [NSNotification notificationWithName:@"PaySuccess" object:nil];
            //发送消息
            [[NSNotificationCenter defaultCenter] postNotification:notice];

        }else{

        }
    }];
    
    
    
}

/*
  微信支付
 */

- (void)startWXPay:(NSString *)timestamp withPartnerId:(NSString *)partnerId withPrepayId:(NSString *)prepayId withNonceStr:(NSString *)nonceStr withPackage:(NSString *)package withSign:(NSString *)sign
{
    //调起微信支付
    PayReq* req             = [[PayReq alloc] init];
    req.partnerId           = partnerId;
    req.prepayId            = prepayId;
    req.nonceStr            = nonceStr;
    req.timeStamp           = timestamp.intValue;
    req.package             = package;
    req.sign                = sign;
    [WXApi sendReq:req];
}






@end
